package com.batdream.message.controller;

import com.batdream.message.pojo.MyFriends;
import com.batdream.message.pojo.Users;
import com.batdream.message.pojo.vo.FriendRequestVo;
import com.batdream.message.service.MyFriendsService;
import com.batdream.message.service.UserService;
import com.batdream.message.service.friendsRequestService;
import com.batdream.message.service.impl.UserServiceImpl;
import com.batdream.message.utils.JSONResult;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@Log4j2
@RestController
@RequestMapping("/friend")
public class FriendController {

    private final friendsRequestService friendsRequestService;
    private final MyFriendsService myFriendsService;
    private final UserService userService;

    @Autowired
    public FriendController(friendsRequestService friendsRequestService, MyFriendsService myFriendsService, UserService userService) {
        this.friendsRequestService = friendsRequestService;
        this.myFriendsService = myFriendsService;
        this.userService = userService;
    }

    /**
     * 接受添加好友请求
     * @param sendUserId 发送请求用户的id
     * @param acceptUserId 接受好友请求的id
     * @return 结果
     */
    @PostMapping("/acceptFriendRequest")
    public JSONResult acceptFriendRequest(String sendUserId,String acceptUserId){
        if (StringUtils.isEmpty(sendUserId)){
            return JSONResult.errorMsg("发送请求用户id错误");
        }
        if (StringUtils.isEmpty(acceptUserId)){
            return JSONResult.errorMsg("接受请求用户id错误");
        }

        try {
            return friendsRequestService.acceptFriendRequest(sendUserId,acceptUserId);
        } catch (Exception e) {
            return JSONResult.errorMsg(e.getMessage());
        }

    }

    /**
     * 删除添加好友请求
     * @param sendUserId 发送请求用户的id
     * @param acceptUserId 接受好友请求的id
     * @return 结果
     */
    @PostMapping("/dropFriendRequest")
    public JSONResult dropFriendRequest(String sendUserId,String acceptUserId){
        return friendsRequestService.dropFriendRequest(sendUserId,acceptUserId);
    }

    /**
     * 检查是否是朋友
     * @param myUserId 用户id
     * @param friendId 对方用户id
     * @return 结果
     */
    @PostMapping("/checkIsNotFriend")
    public JSONResult checkIsNotFriend(String myUserId,String friendId){
        if (StringUtils.isEmpty(myUserId)){
            return JSONResult.errorMsg("用户id错误");
        }
        if (StringUtils.isEmpty(friendId)){
            return JSONResult.errorMsg("朋友id错误");
        }
        return myFriendsService.checkIsNotFriend(myUserId,friendId);
    }

    /**
     * 获取我的朋友列表
     * @param userId 用户id
     * @return 结果
     */
    @PostMapping("/getMyFriends")
    public JSONResult getMyFriends(String userId){
        if (StringUtils.isEmpty(userId)){
            return JSONResult.errorMsg("用户id错误");
        }
        Users users = userService.queryUserById(userId);
        if (Objects.isNull(users)){
            return JSONResult.errorMsg("用户不存在");
        }
        return JSONResult.ok(myFriendsService.getMyFriends(userId));
    }
}
